/*  
 *  ------ [802_04b] - receive packets using a 16-bit source address -------- 
 *  
 *  Explanation: This program shows how to receive packets with 
 *  XBee-802.15.4 modules. The source address is read when the
 *  packet is received
 *  
 *  Copyright (C) 2012 Libelium Comunicaciones Distribuidas S.L. 
 *  http://www.libelium.com 
 *  
 *  This program is free software: you can redistribute it and/or modify 
 *  it under the terms of the GNU General Public License as published by 
 *  the Free Software Foundation, either version 3 of the License, or 
 *  (at your option) any later version. 
 *  
 *  This program is distributed in the hope that it will be useful, 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 *  GNU General Public License for more details. 
 *  
 *  You should have received a copy of the GNU General Public License 
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 *  
 *  Version:           0.1 
 *  Design:            David Gascón 
 *  Implementation:    Yuri Carmona
 */

#include <MakeSureFrame.h>
#include <MakeSureXBee802.h>

// variable to store source's network address
uint8_t network_address[2];
String s;

void setup()
{  
  // init USB port
	Serial.begin(9600);
	Serial.print("Serial_Receive");

  // init XBee 
  xbee802.ON();  
  
  // Set 0x1212 as (MY) Network Address
  xbee802.setOwnNetAddress(0x12,0x12);
  //
  //// write values to XBee module memory
  xbee802.writeValues(); 

}


void loop()
{   
  // check available data in RX buffer
  if( xbee802.available() > 0 ) 
  {
    // parse information
    xbee802.treatData(); 
    
    // check RX flag after 'treatData'
    if( !xbee802.error_RX ) 
    {
      // check available packets
      while( xbee802.pos>0 )
      {
        Serial.println("\n\nNew packet received:"); 
        Serial.println("---------------------------"); 
        
        /*** Available info in 'xbee802.packet_finished' structure ***/
        network_address[0]=xbee802.packet_finished[xbee802.pos-1]->naS[0];
        network_address[1]=xbee802.packet_finished[xbee802.pos-1]->naS[1];
                      
        Serial.print("source network_address:");
        Serial.print(network_address[0], HEX);
        Serial.print(network_address[1], HEX);
        Serial.println();   
       
        Serial.print("Data: ");             
        for( int i=0 ; i < xbee802.packet_finished[xbee802.pos-1]->data_length ; i++)          
        {           
          s=String(xbee802.packet_finished[xbee802.pos-1]->data[i]); 
		  Serial.print(s);
		   
		} 
		for( int i=0 ; i < xbee802.packet_finished[xbee802.pos-1]->data_length ; i++)          
        {  

          s=String(xbee802.packet_finished[xbee802.pos-1]->data[i]); 
		  Serial.print(s);
		}
		Serial.print("\n---------------------------");  
		 		
		           
        free(xbee802.packet_finished[xbee802.pos-1]); //free memory
        xbee802.packet_finished[xbee802.pos-1]=NULL; //free pointer
        xbee802.pos--; //Decrement the received packet counter
		
	  }
    }
  }
} 


